﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using System.Collections;
using System.Reflection;

namespace Portal.Workflow.Utilities
{
    public class PlugInViewFromWorkflowUtilities
    {
        private static IPreviewFromWorkflow[] LoadAssembly(string filename)
        {
            ArrayList ArrayListModules = new ArrayList();
            Assembly a = Assembly.LoadFrom(filename);
            if (a == null) throw new Exception("Assembly " + filename + " not found or not valid");
            foreach (Module mod in a.GetLoadedModules())
            {
                foreach (Type ty in mod.GetTypes())
                {
                    foreach (Type intf in ty.GetInterfaces())
                    {
                        if (intf == typeof(IPreviewFromWorkflow))
                        {
                            IPreviewFromWorkflow w = (IPreviewFromWorkflow)a.CreateInstance(ty.FullName);
                            ArrayListModules.Add(w);
                        }
                    }
                }
            }
            IPreviewFromWorkflow[] ws=new IPreviewFromWorkflow[ArrayListModules.Count];
            for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
            {
                ws[iIndex] = (IPreviewFromWorkflow)ArrayListModules[iIndex];
            }
            return ws;
        }



        static IPreviewFromWorkflow[] LoadPluginPreview(string SiteId,string DllPath)
        {
            string Path = DllPath;
            string[] Files = System.IO.Directory.GetFiles(Path, "*.dll");
            ArrayList ArrayListServives = new ArrayList();
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                //tw.WriteLine(Files[iIndex]);
                try
                {
                    string FileDll = Files[iIndex];
                    IPreviewFromWorkflow[]
                        asms = LoadAssembly(FileDll);
                    for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                    {
                        if (asms[jIndex].Id != null)
                        {
                            ArrayListServives.Add(asms[jIndex]);
                        }
                    }
                }
                catch(Exception ex) 
                {
                    ex.Source = "";
                }
            }
            IPreviewFromWorkflow[] PluginPreview = new IPreviewFromWorkflow[ArrayListServives.Count];
            for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
            {
                PluginPreview[iIndex] = (IPreviewFromWorkflow)ArrayListServives[iIndex];
                //tw.WriteLine(PluginPreview[iIndex].id+" / "+ PluginPreview[iIndex].Title);
            }
            //tw.Close();
            

            return PluginPreview;
        }


        static IPreviewFromWorkflow FindPluginPreview(string SiteId,string DllPath,string id)
        {
            IPreviewFromWorkflow[] found = LoadPluginPreview(SiteId, DllPath);
            for (int iIndex = 0; iIndex < found.Length; iIndex++)
            {
                if (found[iIndex].Id.Trim().ToLower().Equals(id.Trim().ToLower()))
                {
                    return found[iIndex];
                }
            }
            return null;
        }


        static IPreviewFromWorkflow FindPluginPreview(string SiteId, IPreviewFromWorkflow[] IPreviewFromWorkflows, string id)
        {
            for (int iIndex = 0; iIndex < IPreviewFromWorkflows.Length; iIndex++)
            {
                if (IPreviewFromWorkflows[iIndex].Id.Equals(id))
                {
                    return IPreviewFromWorkflows[iIndex];
                }
            }
            return null;
        }

        public static string GetPreviewUrl(string DllPath,string ObjectType, ParamPreviewFromWorkflowCls oParamPreviewFromWorkflowCls)
        {
            IPreviewFromWorkflow[] PreviewFromWorkflows = LoadPluginPreview(oParamPreviewFromWorkflowCls.SiteId, DllPath);
            for (int iIndex = 0; iIndex < PreviewFromWorkflows.Length; iIndex++)
            {
                string Url = PreviewFromWorkflows[iIndex].GetPreviewUrl(ObjectType, oParamPreviewFromWorkflowCls);
                if (string.IsNullOrEmpty(Url) == false) return Url;
            }
            return null;
        }

        public static PlugInMessageCls GetPlugInMessage(string DllPath,string ObjectType, ParamPreviewFromWorkflowCls oParamPreviewFromWorkflowCls)
        {
            IPreviewFromWorkflow[] PreviewFromWorkflows = LoadPluginPreview(oParamPreviewFromWorkflowCls.SiteId, DllPath);
            for (int iIndex = 0; iIndex < PreviewFromWorkflows.Length; iIndex++)
            {
                PlugInMessageCls oPlugInMessageCls = PreviewFromWorkflows[iIndex].GetPlugInMessage(ObjectType, oParamPreviewFromWorkflowCls);
                if (oPlugInMessageCls != null) return oPlugInMessageCls;
            }
            return null;
        }
    }
}
